// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   RadarPlotter.java

package com.java4less.rchart;

import com.java4less.rchart.gc.ChartColor;
import com.java4less.rchart.gc.ChartFont;
import com.java4less.rchart.gc.ChartGraphics;
import com.java4less.rchart.gc.GraphicsProvider;
import com.java4less.rchart.gc.Polygon;
import java.text.DecimalFormat;
import java.util.Vector;

// Referenced classes of package com.java4less.rchart:
//			Plotter, LineStyle, LineDataSerie, ChartLabel, 
//			DataSerie, FillStyle

public class RadarPlotter extends Plotter
{

	public double radiusModifier;
	LineStyle border;
	public boolean drawCircle;
	public String factorNames[];
	public ChartColor factorColors[];
	public double factorMins[];
	public double factorMaxs[];
	public ChartFont factorFont;
	public ChartColor factorColor;
	public LineStyle gridStyle;
	public ChartFont gridFont;
	public ChartColor gridFontColor;
	public int ticks;
	public String tickLabelFormat;
	public FillStyle backStyle;
	public ChartColor pointColors[];
	public double pointColorScale[];

	public RadarPlotter()
	{
		radiusModifier = 0.90000000000000002D;
		border = new LineStyle(0.2F, GraphicsProvider.getColor(ChartColor.BLACK), 1);
		drawCircle = false;
		factorFont = GraphicsProvider.getFont("Arial", ChartFont.PLAIN, 10);
		factorColor = GraphicsProvider.getColor(ChartColor.BLACK);
		gridFontColor = GraphicsProvider.getColor(ChartColor.BLACK);
		ticks = 5;
		tickLabelFormat = "#.#";
		pointColors = null;
		pointColorScale = null;
		combinable = false;
		needsAxis = 0;
	}

	protected void plotSerie(ChartGraphics g, DataSerie s, int serieSec)
	{
		double lastPercent = 0.0D;
		double lastAngle = 0.0D;
		LineDataSerie p;
		if (s instanceof LineDataSerie)
			p = (LineDataSerie)s;
		else
			return;
		s.hotAreas.removeAllElements();
		double count = p.yData.size();
		int xs[] = new int[(int)count];
		int ys[] = new int[(int)count];
		int radi = 0;
		radi = width;
		if (height < radi)
			radi = height;
		radi = (int)((double)radi * radiusModifier);
		int toCenterX = (width - radi) / 2;
		int toCenterY = (height - radi) / 2;
		int PieCenterX = toCenterX + x + radi / 2;
		int PieCenterY = toCenterY + y + radi / 2;
		if (serieSec == 0 || serieSec >= series.size() - 1)
		{
			if (serieSec == 0 && backStyle != null && drawCircle)
				backStyle.drawArc(g, toCenterX + x, toCenterY + y, radi, radi, 0, 360);
			if (serieSec == 0 && backStyle != null && !drawCircle)
			{
				for (int i = 0; (double)i < count; i++)
				{
					double angle = (360D / count) * (double)i;
					angle += 90D;
					if (angle > 360D)
						angle -= 360D;
					double radian = 0.017452777777777779D * angle;
					double Sin = Math.sin(radian);
					double Cos = Math.cos(radian);
					int relativeY = (int)(Sin * (double)(radi / 2));
					int relativeX = (int)(Cos * (double)(radi / 2));
					relativeY *= -1;
					xs[i] = PieCenterX + relativeX;
					ys[i] = PieCenterY + relativeY;
				}

				backStyle.drawPolygon(g, xs, ys, (int)count);
			}
			if (serieSec == series.size() - 1 && border != null)
			{
				if (drawCircle)
					border.drawArc(g, toCenterX + x, toCenterY + y, radi, radi, 0, 360);
				for (int i = 0; (double)i < count; i++)
				{
					double angle = (360D / count) * (double)i;
					angle += 90D;
					if (angle > 360D)
						angle -= 360D;
					double radian = 0.017452777777777779D * angle;
					double Sin = Math.sin(radian);
					double Cos = Math.cos(radian);
					int relativeY = (int)(Sin * (double)(radi / 2));
					int relativeX = (int)(Cos * (double)(radi / 2));
					relativeY *= -1;
					border.draw(g, PieCenterX, PieCenterY, PieCenterX + relativeX, PieCenterY + relativeY);
				}

			}
			if (serieSec == 0 && factorFont != null)
			{
				g.setFont(factorFont);
				for (int i = 0; (double)i < count; i++)
				{
					g.setColor(factorColor);
					if (factorColors != null && factorColors.length > i)
						g.setColor(factorColors[i]);
					double angle = (360D / count) * (double)i;
					angle += 90D;
					if (angle > 360D)
						angle -= 360D;
					int tmpradi = (int)(((double)radi * 1.1000000000000001D) / 2D);
					int correction = 0;
					if (angle > 120D && angle < 240D)
						correction = g.getFontWidth(factorNames[i]);
					double radian = 0.017452777777777779D * angle;
					double Sin = Math.sin(radian);
					double Cos = Math.cos(radian);
					int relativeY = (int)(Sin * (double)tmpradi);
					int relativeX = (int)(Cos * (double)tmpradi);
					relativeY *= -1;
					if (factorNames.length <= i)
						continue;
					if (factorNames[i].indexOf("@") >= 0)
					{
						ChartLabel label = new ChartLabel(factorNames[i], "", false, false);
						label.initialize(g, super.chart);
						label.paint(g, (PieCenterX + relativeX) - correction, PieCenterY + relativeY, -1, -1);
					} else
					{
						g.drawString(factorNames[i], (PieCenterX + relativeX) - correction, PieCenterY + relativeY);
					}
				}

			}
		}
		for (int i = 0; (double)i < count; i++)
		{
			double angle = (360D / count) * (double)i;
			angle += 90D;
			if (angle > 360D)
				angle -= 360D;
			int tmpradi = 0;
			double min = 0.0D;
			double max = 100D;
			if (factorMins.length >= i + 1)
				min = factorMins[i];
			if (factorMaxs.length >= i + 1)
				max = factorMaxs[i];
			tmpradi = (int)(((((Double)p.getElementY(i)).doubleValue() - min) * 100D) / (max - min));
			tmpradi = (tmpradi * radi) / 100;
			double radian = 0.017452777777777779D * angle;
			double Sin = Math.sin(radian);
			double Cos = Math.cos(radian);
			int relativeY = (int)(Sin * (double)(tmpradi / 2));
			int relativeX = (int)(Cos * (double)(tmpradi / 2));
			relativeY *= -1;
			xs[i] = PieCenterX + relativeX;
			ys[i] = PieCenterY + relativeY;
		}

		if (p.style != null)
			p.style.drawPolygon(g, xs, ys, (int)count);
		if (p.fillStyle != null)
			p.fillStyle.drawPolygon(g, xs, ys, (int)count);
		for (int i = 0; (double)i < count; i++)
		{
			Polygon po = new Polygon();
			po.addPoint(xs[i] - 3, ys[i] - 3);
			po.addPoint(xs[i] - 3, ys[i] + 3);
			po.addPoint(xs[i] + 3, ys[i] + 3);
			po.addPoint(xs[i] + 3, ys[i] - 3);
			s.hotAreas.addElement(po);
			double YValue;
			if (p.drawPoint)
			{
				ChartColor c = p.pointColor;
				YValue = ((Double)p.getElementY(i)).doubleValue();
				if (pointColors != null && pointColorScale != null)
				{
					if (pointColors.length > 0)
						c = pointColors[0];
					for (int j = 1; j < pointColors.length; j++)
					{
						if (pointColorScale.length < j)
							continue;
						if (pointColorScale[j - 1] > YValue)
							break;
						c = pointColors[j];
					}

				}
				g.setColor(c);
				if (p.icon == null)
					g.fillRect(xs[i] - 3, ys[i] - 3, 6, 6);
				else
					g.drawImage(p.icon, xs[i] - 4, ys[i] - 4);
			}
			if (p.valueFont == null)
				continue;
			g.setColor(p.valueColor);
			g.setFont(p.valueFont);
			YValue = ((Double)p.getElementY(i)).doubleValue();
			String txt = p.doubleToString(new Double(YValue));
			if (YValue == (double)(int)YValue)
				txt = (new Integer((int)YValue)).toString();
			String txtValue = txt;
			if (p.labelTemplate.length() > 0)
				txt = p.labelTemplate;
			if (p.dataLabels != null && p.dataLabels.length > i)
				txt = p.dataLabels[i];
			if (txt.indexOf("@") >= 0)
			{
				ChartLabel label = new ChartLabel(txt, txtValue, false, false);
				label.initialize(g, super.chart);
				label.paint(g, xs[i] + 7, ys[i], -1, -1);
			} else
			{
				g.drawString(txt, xs[i] + 7, ys[i]);
			}
		}

		if (gridStyle != null)
		{
			double min = 0.0D;
			double max = 100D;
			if (factorMins.length >= 1)
				min = factorMins[0];
			if (factorMaxs.length >= 1)
				max = factorMaxs[0];
			int tickInterval = 100 / ticks;
			double tickIntervalAbs = (max - min) / (double)ticks;
			int tickAt = 0;
			double tickAtAbs = 0.0D;
			for (int j = 0; j < ticks; j++)
			{
				tickAt += tickInterval;
				tickAtAbs += tickIntervalAbs;
				for (int i = 0; (double)i < count; i++)
				{
					double angle = (360D / count) * (double)i;
					angle += 90D;
					if (angle > 360D)
						angle -= 360D;
					int tmpradi = (radi * tickAt) / 100;
					double radian = 0.017452777777777779D * angle;
					double Sin = Math.sin(radian);
					double Cos = Math.cos(radian);
					int relativeY = (int)(Sin * (double)(tmpradi / 2));
					int relativeX = (int)(Cos * (double)(tmpradi / 2));
					relativeY *= -1;
					xs[i] = PieCenterX + relativeX;
					ys[i] = PieCenterY + relativeY;
				}

				if (serieSec >= super.series.size() - 1)
					gridStyle.drawPolygon(g, xs, ys, (int)count);
				if (serieSec < super.series.size() - 1 || gridFont == null)
					continue;
				g.setColor(gridFontColor);
				g.setFont(gridFont);
				double tickValue = tickAtAbs;
				String v = "";
				v = (new StringBuilder()).append("").append(tickValue).toString();
				if (tickValue == (double)(int)tickValue)
					v = (new StringBuilder()).append("").append((int)tickValue).toString();
				if (tickLabelFormat.length() > 0)
				{
					DecimalFormat df = null;
					df = new DecimalFormat(tickLabelFormat);
					v = df.format(new Double(tickValue));
				}
				g.drawString((new StringBuilder()).append("").append(v).toString(), xs[0] - 3 - g.getFontWidth((new StringBuilder()).append("").append(v).toString()), ys[0]);
			}

		}
	}
}
